package com.zmj.framework.util;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.jdbc.core.RowMapper;

public class GeneralRowMapper implements RowMapper {
	private Class targartObjectClass;

	public GeneralRowMapper(Class objectClass) {
		this.targartObjectClass = objectClass;
	}

	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		Object t = null;
		ResultSetMetaData crmd = rs.getMetaData();
		Map map = new HashMap();
		for (int j = 1; j <= crmd.getColumnCount(); ++j)
			map.put(crmd.getColumnName(j).toLowerCase(), rs.getObject(crmd.getColumnName(j).toLowerCase()));
		try {
			t = this.targartObjectClass.newInstance();
			BeanUtils.populate(t, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}
}